package tpf.designer.pattern.course7.behavior_pattern2.homework.exercise2;

import java.util.Random;

public class LoginStrategyObj {

    /**
     * 持有一个具体的策略对象
     */
    private LoginStrategy loginStrategy;

    private static final LoginStrategy[] loginStrategies = new LoginStrategy[]{
            new PasswordLoginStrategy(),
            new ScancodeLoginStrategy()
    };

    /**
     * 构造函数，传入一个具体的策略对象
     * @param loginStrategy
     */
    /*public LoginStrategyObj(LoginStrategy loginStrategy) {
        this.loginStrategy = loginStrategy;
    }*/

    /**
     *
     * @return
     */
    public boolean selectLoginComponent(){
        return this.loginStrategy.doLogin();
    }

    /**
     *
     * @return
     */
    private  LoginStrategy chooseLoginType(){
        Random r = new Random();
        int i = r.nextInt(loginStrategies.length);
        return loginStrategies[i];
    }

    /**
     *
     * @return
     */
    public boolean login(){
        LoginStrategy s = chooseLoginType();
        return s.doLogin();
    }
}
